package util;


import java.util.List;

public class Condition {
    public static String toWhereClause(List<Condition> conditionList){

        //以WHERE开头，前后要有空格
        StringBuilder sb =new StringBuilder(" WHERE ");
        //将字段名，操作符和字段值依次拼接为字串
        //每个条件后都附加AND，前后有空格
        for (Condition c : conditionList) {
            sb.append(c.getKey()).append(" ").append(c.getOperator()).append(" ");
            String value = c.getValue();
            //如果操作符是like，自动设置为模糊匹配
            if("like".equalsIgnoreCase(c.getOperator())){
                value = "%" +value + "%";
            }
            value =  TxtHelper.quoteMarked(value);
            sb.append(value+" ");
            sb.append(" AND ");
        }
        //把最后一个AND删除
        TxtHelper.truncEnding(sb,5);
        return sb.toString();
    }
    public Condition(String key,String operator,String value){
        this.key = key;
        this.operator = operator;
        this.value = value;
    }
    public String getKey(){
        return key;
    }
    public String getOperator(){
        return operator;
    }
    public String getValue(){
        return value;
    }
    //字段名
    private String key;
    //操作符
    private String operator;
    //字段值
    private String value;
}
